<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@include file="../../common/operation_forbid.jsp" %>
<%@include file="../../common/jsVersion.jsp"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<script type="text/javascript">
	var returnUrl = '${returnUrl}';
    var contextPath = '${contextPath}';
    var documentType=${documentType};
    var paymentMethods=${paymentMethods};
    var contactTypes=${contactTypes};
	var haveReceivedPayDepositAmount=0;
    var customerId = '${customerId}';
    var roomId = '${roomId}';
    var closeTabRoomId = roomId;
    var userId = '${userId}';
    var payDepositId = '${payDeposit.id}';
    var subscribeId='${subscribe.id}';
    var subscribeCode="${subscribe.subscribeCode}";
    var agentId1="${subscribe.agentId}";
    var agentId2="${payDeposit.agentId}";
    var payDepositCode="${payDeposit.depositCode}";
    var objectAmount = JSON.parse('${signedContractAmount}');
    var menu = '${menu}';
    var token = '${token}';
    var url = '${url}';
    var projectId = '${projectId}';
    //认筹金额
    var payAepositAmount=0;
    //认购金额
    var subscribeAmount=0;
    if(payDepositId!=""){
    	payAepositAmount=objectAmount[0].payAepositAmount;
    }
    if(subscribeId!=""){
    	payAepositAmount=objectAmount[0].payAepositAmount;
    	subscribeAmount=objectAmount[0].subscribeAmount;
    	payDepositCode="${subscribe.payDepositCode}";
    }
    if(payAepositAmount==""){
    	payAepositAmount=0;
    }
    if(subscribeAmount==""){
    	subscribeAmount=0;
    }
    //已收认购金
    haveReceivedPayDepositAmount=subscribeAmount;
    var totalHaveReceivedAmount=parseInt(subscribeAmount)+parseInt(payAepositAmount);
    var contractAmountName;
    if(${payDepositOff}){
    	contractAmountName="认购金:"+subscribeAmount+"合计:"+objectAmount[0].total;
    }else{
    	contractAmountName="认筹金:"+payAepositAmount+"认购金:"+subscribeAmount+"合计:"+objectAmount[0].total;
    }
     
    
    //totalNeedReceivedAmount
    var totalAmount = objectAmount[0].total;
    
    var customerProjectId='${customerProjectId}';
    var showChooseCustomer=${showChooseCustomer};
    var receivable = 30;
    
</script>
<!DOCTYPE html >
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>签约</title>
    <!-- neptune -->
    <%@include file="../../common/extInclude.jsp" %>
    <script type="text/javascript" src="${contextPath}/static/js/components/customerComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/components/estateComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/components/userComponent.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/customer/signed/add.js?v=${jsVersion}"></script>
    <script type="text/javascript" src="${contextPath}/static/js/jquery/json2.js"></script> 
    <script type="text/javascript" src="${contextPath}/static/js/components/fileComponent.js?v=${jsVersion}"></script>
</head>
<script type="text/javascript">
var userIdStore = null;
var addPanel = null;
Ext.onReady(function() {
	Ext.Ajax.timeout = 180000;  
    Ext.define('Record', { 
        extend: 'Ext.data.Model',
        fields: ["relationAgentid","proportionCommission"]
    }); 
	
    userIdStore = new Ext.data.Store({
    	        fields: ["userName", "id"],
    	        proxy:{
    	            type:'ajax',
    	            url:contextPath+'/customer/customer/userByProjectIdForGroup',
    	            reader:{
                        type: 'json', 
                        root: 'root'  //数据
                   }
    	        },
    			autoLoad:false
    });
	Ext.tip.QuickTipManager.init()
	addPanel=Ext.create("customer.signed.addPanel");
	if(roomId!="" && null!=roomId){
		findRoom(roomId,null,null);
		Ext.getCmp("changeHouse").setVisible(false);
	}
	if(customerId!="" && null!=customerId){
		findCustomer(customerId);
		Ext.getCmp("changeCustomerBtn").setVisible(false);
/* 		userIdStore.load({
    	  	params:{
    		  	'projectId':customerProjectId
    	  	}
        }); */
	}
	
	//认购 转签约
	if(subscribeId!=""){
		 Ext.getCmp("changeHouse").setVisible(false);
		 Ext.getCmp("changeCustomerBtn").setVisible(false);
		//客户信息
		var cId = '${subscribe.customerId}';
		var rId = '${subscribe.roomId}';
		findCustomer(cId);
		//房间信息从认购信息中获取
		findRoom(rId,subscribeId,null);

	}
	//认筹转签约
	if(payDepositId!=""){
		//客户信息
		var cId = '${payDeposit.customerId}';
		findCustomer(cId);
		//房间信息
		var rId = '${payDeposit.roomId}';
		
		findRoom(rId,null,payDepositId);
		//认筹转认购，隐藏选择房间按钮,如果是无房认筹,就可以选择
		var isPaydeposit = '${payDeposit.isPaydeposit}';
		if(isPaydeposit=="0"){
			 Ext.getCmp("changeHouse").setVisible(false);
			 Ext.getCmp("changeCustomerBtn").setVisible(false);
		} 
	}
});


function findCustomer(customerId){
	//初始化房间信息
 	 Ext.Ajax.request({
	    url: contextPath+'/customer/customer/customerDetail',
	    params: {
	    	id: customerId
	    },
	    method: 'POST',
	    success: function(response){
	        var text = Ext.decode(response.responseText);
	        if(text.res=="1"){
	        	 //初始化客户信息
	     		  addPanel.getComponent("customerPanel").getForm().setValues(text.obj);
	     		  var familyMemberGridStore=addPanel.getComponent("customerPanel").getComponent("familyMemberGridPanel").getStore();
	     		  var extraParams=familyMemberGridStore.getProxy().extraParams;
	     		  if(payDepositId!="" || subscribeId!=""){
	     			  //当认筹ID不为空则加载认筹的客户成员信息
	     			   if(subscribeId!=""){
	     				 extraParams.buyId=subscribeId;
   		 	             extraParams.memberType='3';
    		 	          userIdStore.load({
      		 	        	  params:{
       		 	        		  'projectId':text.obj.projectId
       		 	        	  },
     		 	        	  	callback:function(){
       		 	        	  		var index = userIdStore.find('id',userId);  
			                        if(index!=-1){  
			                            var record  = new Record({
			                            	relationAgentid:userId,
			                            	proportionCommission:'100'
			                            }); 
			                            gridStoreJingJ.insert(0, record);
			                			Ext.getCmp("proportionCommission").setVisible(false);
			                        }
       		 	        	  	}
       		 	          });
       		 	         // Ext.getCmp('userName').setValue(agentId1);
	     			  }else if(payDepositId!=""){
	     				 extraParams.buyId=payDepositId;
   		 	             extraParams.memberType='2';
    		 	          userIdStore.load({
      		 	        	  params:{
       		 	        		  'projectId':text.obj.projectId
       		 	        	  },
     		 	        	  	callback:function(){
       		 	        	  		var index = userIdStore.find('id',userId);
			                        if(index!=-1){
			                            var record  = new Record({
			                            	relationAgentid:userId,
			                            	proportionCommission:'100'
			                            });
			                            gridStoreJingJ.insert(0, record);
			                			Ext.getCmp("proportionCommission").setVisible(false);
			                        }
       		 	        	  	}
       		 	          });
	     			  }
	     			  
	     		  }else{
	     			 extraParams.customerId=customerId;
	     			 extraParams.memberType='3';
   		 	         userIdStore.load({
  		 	        	  params:{
   		 	        		  'projectId':text.obj.projectId
   		 	        	  },
 		 	        	  	callback:function(){
   		 	        	  		var index = userIdStore.find('id',userId);
		                        if(index!=-1){
		                            var record  = new Record({
		                            	relationAgentid:userId,
		                            	proportionCommission:'100'
		                            });
		                            gridStoreJingJ.insert(0, record);
		                			Ext.getCmp("proportionCommission").setVisible(false);
		                        }
   		 	        	  	}
   		 	         });
	     		  }
 		 	     familyMemberGridStore.load();
	        }
	    }
	});
}

function findRoom(roomId,subId,payId){
	var reqUrl = "";
	if(subId!=null){
		reqUrl=contextPath+"/estate/projectRoom/findSubscribe?roomId="+roomId+"&subscribeId="+subId;
	}else if(payId!=null){
		reqUrl=contextPath+"/estate/projectRoom/findPayDeposit?roomId="+roomId+"&paydepositId="+payId;
	}else{
		reqUrl=contextPath+'/estate/projectRoom/roomDetail?id='+roomId;
	}
	//初始化房间信息
	 Ext.Ajax.request({
	    url:reqUrl ,
	    method: 'POST',
	    success: function(response){
	        var text = Ext.decode(response.responseText);
	        if(text.res=="1"){
        	  	addPanel.getComponent("estatePanel").getForm().setValues(text.obj);
	        	var subscribeInfoForm=Ext.getCmp("subscribeInfoForm").getForm();
        	  	//设置折扣信息
       		  	//从房源信息里面获取
       		  	var result=text.obj;
       		  	//折前单价  折前总价 折扣金额  折扣比例 折扣说明 签约单价 合同金额 认筹金额 认购金额 经济人
       		  	var values={};
       		  	values.discountAfterPrice=result.discountAfterPrice;
       		  	if(!(result.discountAfterPrice)){
      				values.discountAfterPrice=result.totalPrice;
       		  	}
       		  	values.floorType=result.floorType;
       		  	//折前单价
       		  	values.calculateUnitPrice=result.calculateUnitPrice;
       		  	//折前总价 
       		  	values.totalPrice=result.totalPrice;
       		  	//折扣金额
	        	if(values.discountAfterPrice!=null){
	        		values.discountAfterAmount=0;//result.totalPrice-result.discountAfterPrice;
       		  	}
       		  	if(result.floorType=="1"){
          			values.discountRate = 100;
  	        		var minDiscount = 100 - result.discountRate;
 	        		values.discountRateDisplay="(备注: 折扣 >= <font color='red'>" + minDiscount + "%</font>,将发起折扣审批)";
   	        	 	//隐藏折后总价、显示折扣比例
 	        		Ext.getCmp('discountAfterPrice').setVisible(false);
   	        	 	Ext.getCmp("discountRate").setVisible(true);
   	        	 	subscribeInfoForm.findField("discountAfterAmount").setReadOnly(false);
   	        	}else if(result.floorType=="0"){
   	        		values.discountAfterAmount=0;
   	        		values.discountRateDisplay="注：折后总价低于"+result.discountRate+"元将发起购房折扣审批，且审批完成后才生效";
  	  	        	//显示折后总价、隐藏折扣比例
  	  	        	Ext.getCmp('discountAfterPrice').setVisible(true);
  	  	        	Ext.getCmp("discountRate").setVisible(false);
 	        		subscribeInfoForm.findField("discountAfterAmount").setReadOnly(true);
	  	  	   	}
	        		  
	       		values.discountRateHidden=result.discountRate;
       		  	//合同金额
       		  	if(result.floorType=="0"){
      			  	values.discountTotalAmount=result.discountTotalAmount;
       		  	}
	        		  
       		  	//已收总金额
       		  	values.contractAmountName=contractAmountName;
       		  	//未收总金额
       		  	values.totalNeedReceivedAmount=result.totalPrice-totalHaveReceivedAmount;
 	          	values.hetongPayPrice = totalHaveReceivedAmount;
 		 	          
 	          	subscribeInfoForm.setValues(values);
 	          	subscribeInfoForm.findField("isCalculate").setValue("1"); 
				subscribeInfoForm.findField("remark").setValue("");
			}
		}
	});
}

</script>
<body>
</body>
</html>